﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using CMS.Contracts;
using CMS.Model;
using System.Transactions;

namespace CMS.Services
{
    public class ProjectBService : IProjectBService
    {
        /// <summary>
        /// Gets all teachers.
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowCount"></param>
        /// <returns>
        /// List of Teacher
        /// </returns>
        public List<CMS.Model.Teacher> GetAllTeachers(int pageIndex, int pageSize, out int rowCount)
        {
            Controller.Controller controller = new Controller.Controller();
            return controller.GetAllTeachers(pageIndex, pageSize, out rowCount);
        }
        /// <summary>
        /// Gets all teacher.
        /// </summary>
        /// <returns></returns>
        public List<CMS.Model.Teacher> GetAllTeacher()
        {
            Controller.Controller controller = new Controller.Controller();
            return controller.GetAllTeachers();
        }


        /// <summary>
        /// Gets the teacher by id.
        /// </summary>
        /// <param name="teacherId">The teacher id.</param>
        /// <returns>
        /// List of Teacher
        /// </returns>
        public List<CMS.Model.Teacher> GetTeacherById(int teacherId)
        {
            Controller.Controller controller = new Controller.Controller();
            return controller.GetTeacherById(teacherId);
        }

        /// <summary>
        /// Adds the teacher.
        /// </summary>
        /// <param name="teacher"></param>
        public void AddTeacher(CMS.Model.Teacher teacher)
        {
            Controller.Controller controller = new Controller.Controller();
            controller.AddTeacher(teacher);
        }

        /// <summary>
        /// Updates the teacher.
        /// </summary>
        /// <param name="teacher">The teacher.</param>
        public void UpdateTeacher(CMS.Model.Teacher teacher)
        {
            Controller.Controller controller = new Controller.Controller();
            controller.UpdateTeacher(teacher);
        }

        /// <summary>
        /// Deletes the teacher by id.
        /// </summary>
        /// <param name="teacherId">The teacher id.</param>
        public void DeleteTeacherById(int teacherId)
        {
            Controller.Controller controller = new Controller.Controller();
            controller.DeleteTeacherById(teacherId);
        }

        /// <summary>
        /// Gets all departments.
        /// </summary>
        /// <returns></returns>
        public List<CMS.Model.Department> GetAllDepartments()
        {
            Controller.Controller controller = new Controller.Controller();
            return controller.GetAllDepartments();
        }

        /// <summary>
        /// Gets the department by id.
        /// </summary>
        /// <param name="departmentId">The department id.</param>
        /// <returns></returns>
        public List<CMS.Model.Department> GetDepartmentById(int departmentId)
        {
            Controller.Controller controller = new Controller.Controller();
            return controller.GetDepartmentById(departmentId);
        }

        /// <summary>
        /// Adds the department.
        /// </summary>
        /// <param name="department">The department.</param>
        public void AddDepartment(CMS.Model.Department department)
        {
            Controller.Controller controller = new Controller.Controller();
            controller.AddDepartment(department);
        }

        /// <summary>
        /// Updates the department.
        /// </summary>
        /// <param name="department">The department.</param>
        public void UpdateDepartment(CMS.Model.Department department)
        {
            Controller.Controller controller = new Controller.Controller();
            controller.UpdateDepartment(department);
        }

        /// <summary>
        /// Deletes the department by id.
        /// </summary>
        /// <param name="teacherId">The teacher id.</param>
        public void DeleteDepartmentById(int teacherId)
        {
            Controller.Controller controller = new Controller.Controller();
            controller.DeleteDepartmentById(teacherId);
        }

        /// <summary>
        /// Gets all students.
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowCount"></param>
        /// <returns>
        /// List of Student
        /// </returns>
        public List<CMS.Model.Student> GetAllStudents(int pageIndex, int pageSize, out int rowCount)
        {
            Controller.Controller controller = new Controller.Controller();
            return controller.GetAllStudents(pageIndex, pageSize, out rowCount);
        }

        /// <summary>
        /// Gets the student by id.
        /// </summary>
        /// <param name="studentId">The student id.</param>
        /// <returns>
        /// List of Student
        /// </returns>
        public CMS.Model.Student GetStudentById(int studentId)
        {
            Controller.Controller controller = new Controller.Controller();
            return controller.GetStudentById(studentId);
        }

        /// <summary>
        /// Adds the student.
        /// </summary>
        /// <param name="student">The student.</param>
        public void AddStudent(CMS.Model.Student student)
        {
            Controller.Controller controller = new Controller.Controller();
            controller.AddStudent(student);
        }

        /// <summary>
        /// Updates the st cudent.
        /// </summary>
        /// <param name="student">The student.</param>
        public void UpdateStudent(CMS.Model.Student student)
        {
            Controller.Controller controller = new Controller.Controller();
            controller.UpdateStudent(student);
        }

        /// <summary>
        /// Deletes the student by id.
        /// </summary>
        /// <param name="studentId">The student id.</param>
        public void DeleteStudentById(int studentId)
        {

            try
            {
                Controller.Controller controller = new Controller.Controller();
                controller.DeleteStudentById(studentId);
            }
            catch (InvalidOperationException)
            {
                InvalidOperationException ioe = new InvalidOperationException();
                throw new FaultException<InvalidOperationException>(ioe, "Cascading deleted shows");
            }
        }

        /// <summary>
        /// Gets all grades.
        /// </summary>
        /// <returns>
        /// List of Grade
        /// </returns>
        public List<Grade> GetAllGrades()
        {
            Controller.Controller controller = new Controller.Controller();
            return controller.GetAllGrades();
        }

        /// <summary>
        /// Gets the course by id.
        /// </summary>
        /// <param name="courseId">The course id.</param>
        /// <returns></returns>
        public List<CMS.Model.Course> GetCourseById(int courseId)
        {
            Controller.Controller controller = new Controller.Controller();
            return controller.GetCourseById(courseId);
        }

        /// <summary>
        /// Adds the course.
        /// </summary>
        /// <param name="course">The course.</param>
        /// <returns></returns>
        public int AddCourse(CMS.Model.Course course)
        {
            Controller.Controller controller = new Controller.Controller();
            return controller.AddCourse(course);
        }

        /// <summary>
        /// Adds the course detail.
        /// </summary>
        /// <param name="courseDetail">The course detail.</param>
        public void AddCourseDetail(CMS.Model.CourseDetails courseDetail)
        {
            Controller.Controller controller = new Controller.Controller();
            controller.AddCourseDetails(courseDetail);
        }

        /// <summary>
        /// Updates the course.
        /// </summary>
        /// <param name="course">The course.</param>
        public void UpdateCourse(CMS.Model.Course course)
        {
            Controller.Controller controller = new Controller.Controller();
            controller.UpdateCourse(course);
        }

        /// <summary>
        /// Deletes all students by course id.
        /// </summary>
        /// <param name="courseId">The course id.</param>
        public void DeleteAllStudentsByCourseId(int courseId)
        {
            Controller.Controller controller = new Controller.Controller();
            controller.DeleteAllStudentsByCourseId(courseId);
        }

        /// <summary>
        /// Gets the students in course.
        /// </summary>
        /// <param name="courseId">The course id.</param>
        /// <returns></returns>
        public List<CMS.Model.Student> GetStudentsInCourse(int courseId)
        {
            Controller.Controller controller = new Controller.Controller();
            return controller.GetStudentsInCourse(courseId);
        }

        /// <summary>
        /// Gets the students not in course.
        /// </summary>
        /// <param name="courseId">The course id.</param>
        /// <returns></returns>
        public List<CMS.Model.Student> GetStudentsNotInCourse(int courseId)
        {
            Controller.Controller controller = new Controller.Controller();
            return controller.GetStudentsNotInCourse(courseId);
        }

        /// <summary>
        /// Gets all levels.
        /// Author:Fiona Pan
        /// </summary>
        /// <returns>
        /// List of Level
        /// </returns>
        public List<CMS.Model.Level> GetAllLevels()
        {
            Controller.Controller controller = new Controller.Controller();
            return controller.GetAllLevels();
        }

        /// <summary>
        /// Gets all courses.
        /// Author:Fiona Pan
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowCount"></param>
        /// <returns>
        /// List of Course
        /// </returns>
        public List<CMS.Model.Course> GetAllCourses(int pageIndex, int pageSize, out int rowCount)
        {
            Controller.Controller controller = new Controller.Controller();
            return controller.GetAllCourses(pageIndex, pageSize, out rowCount);
        }

        /// <summary>
        /// Gets courses by condition.
        /// Author:Fiona Pan
        /// </summary>
        /// <param name="courseName">string CourseName</param>
        /// <param name="courseStatus">bool CourseStatus</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowCount"></param>
        /// <returns>
        /// List of Course
        /// </returns>
        public List<CMS.Model.Course> GetCoursesByCondition(string courseName, string courseStatus, int pageIndex, int pageSize, out int rowCount)
        {
            Controller.Controller controller = new Controller.Controller();
            return controller.GetCoursesByCondition(courseName, courseStatus, pageIndex, pageSize, out rowCount);
        }

        /// <summary>
        /// Deletes course by id
        /// Author:Fiona Pan
        /// </summary>
        /// <param name="courseId">int courseId</param>
        public void DeleteCourseById(int courseId)
        {
            Controller.Controller controller = new Controller.Controller();
            controller.DeleteCourseById(courseId);
        }

        /// <summary>
        /// Generates course report.
        /// Author:Fiona Pan
        /// </summary>
        /// <param name="condition">the given condition</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowCount"></param>
        /// <returns>
        /// List of courselist
        /// </returns>
        public List<CourseList> GenerateCourseReport(Condition condition, int pageIndex, int pageSize, out int rowCount)
        {
            Controller.Controller controller = new Controller.Controller();
            return controller.GenerateCourseReport(condition, pageIndex, pageSize, out rowCount);
        }
        /// <summary>
        /// Changes coursestatus to false.
        /// Uses transactionscope.
        /// </summary>
        /// <param name="inputCourseList">List of course.</param>
        public void ChangeCourseStatus(Course inputCourse)
        {
            using (TransactionScope transactionScope = new TransactionScope())
            {
                Controller.Controller controller = new Controller.Controller();
                controller.UpdateCourseStatus(inputCourse);
                transactionScope.Complete();
            }

        }

    }
}
